<template>
  <div class="app-container">
    <el-page-header @click="bindBack" :icon="ArrowLeft" class="header">
      <template #content>
        <span class="text-large font-600 mr-3"> 整改提升/ {{route.query.type==2?'自查':"检查内容上报"}}</span>
      </template>
    </el-page-header>
    <el-row :gutter="10" class="mb8">
      <el-col :span="22">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
          <el-form-item label="检查时间" prop="checkDate">
            <el-date-picker v-model="queryParams.checkDate" value-format="YYYY-MM-DD" type="date" range-separator="-" start-placeholder="开始日期"
              end-placeholder="结束日期"></el-date-picker>
          </el-form-item>

          <el-form-item label="收费站" prop="deptId">
            <el-tree-select v-model="queryParams.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
              :disabled="true" class="margin-right" placeholder="请选择部门" check-strictly clearable @change="bindChangeTree($event,'deptId')" />
          </el-form-item>

          <el-form-item label="检查类型" prop="checkType">
            <el-select v-model="queryParams.checkType" :disabled="true">
              <el-option v-for="dict in check_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>

          </el-form-item>

        </el-form>
      </el-col>
      <el-col :span="1.5">

        <el-button type="primary" plain @click="getList">刷新</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="projectList" show-overflow-tooltip>
      <el-table-column type="index" width="65" label="序号" align="center" />
      <el-table-column label="类别" align="center" prop="categoryType">
        <template #default="scope">
          <dict-tag :options="check_category" :value="scope.row.categoryType" />
        </template>
      </el-table-column>
      <el-table-column label="项目" align="center" prop="checkProject" />
      <el-table-column label="检查内容" align="center" prop="checkContent" />
      <el-table-column label="通过/不通过" align="center" prop="isPass" width="180" v-if="route.query.type==1">
        <template #default="scope">
          <span>{{ scope.row.isPass==1?'通过':'不通过' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="不通过原因" align="center" prop="reason" v-if="route.query.type==1" />

      <el-table-column label="通过/不通过" align="center" prop="isPass" width="180" v-if="route.query.type==2">
        <template #default="scope">
          <el-select v-model="scope.row.isPass">
            <el-option v-for="dict in possList" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="不通过原因" align="center" prop="reason" width="180" v-if="route.query.type==2">
        <template #default="scope">
          <el-input v-model="scope.row.reason" placeholder="请输入" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100px" v-if="route.query.type==1">
        <template #default="scope">
          <el-button size="small" link type="danger" @click="removeListener( scope.$index)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button style="width:100%" type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['coordination:improvement:add']"
      v-if="route.query.type==1">新增</el-button>

    <div class="flex align-center margin">
      <el-button type="success" plain @click="submitForm" v-hasPermi="['coordination:improvement:add']">保存</el-button>
      <el-button plain @click="bindBack" v-hasPermi="['coordination:improvement:add']">取消</el-button>
    </div>

    <!-- 添加或修改整改信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="improvementRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="类别" prop="categoryType">
          <el-select style="width:100%" v-model="form.categoryType">
            <el-option v-for="dict in check_category" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目" prop="checkProject">
          <el-input v-model="form.checkProject" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="检查内容" prop="checkContent">
          <el-input v-model="form.checkContent" type="textarea" placeholder="请输入" rows="6" />
        </el-form-item>
        <el-form-item label="检查结果" prop="isPass">
          <el-select style="width:100%" v-model="form.isPass" @change="bindChangePass">
            <el-option v-for="dict in possList" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="不通过原因" prop="reason" v-if="isReason">
          <el-input v-model="form.reason" type="textarea" placeholder="请输入" rows="6" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="bindProjectSubmit">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Improvement">
import { listImprovement, getImprovement, delImprovement, addImprovement, updateImprovement, addImprovementList } from "@/api/coordination/improvement";
import { getCKInfo } from '@/utils/auth'

import { deptTreeSelect } from "@/api/system/user";
import { watch } from "vue";
const { proxy } = getCurrentInstance();
const { check_type, check_category } = proxy.useDict('check_type', 'check_category');
const router = useRouter()
const route = useRoute()
const improvementList = ref([]);
const open = ref(false);
const loading = ref(false);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const possList = ref([{ label: "通过", value: "1" }, { label: "不通过", value: "0" }])
const deptId = ref(Number(getCKInfo('deptId')))
const deptOptions = ref()
const projectList = ref([])
const isReason = ref(false)
const details = ref({})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sourceDeptId: route.query.type == 1 ? deptId.value : Number(route.query.deptId),
    checkDate: null,
    checkType: route.query.type,
    deptId: deptId.value,
  },
  rules: {
  }
});


const { queryParams, form, rules } = toRefs(data);

watch(() => route.query.id, val => {
  getList()
}, { deep: true })

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
}


const bindChangePass = (val) => {
  isReason.value = val === '1' ? false : true
}
const removeListener = (index) => {
  projectList.value.splice(index, 1)
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    deptId: route.query.type == 1 ? deptId.value : Number(route.query.deptId),
    checkDate: null,
    checkType: route.query.type,
    sourceDeptId: route.query.type == 1 ? deptId.value : Number(route.query.deptId),
  };
  proxy.resetForm("improvementRef");
}





/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  isReason.value = false;
  title.value = "添加检查内容";
}


function getList() {
  projectList.value = [];
  let _id = route.query.id;

  if (!_id) { return; }
  getImprovement(_id).then(response => {
    // loading.value = false;
    details.value = response.data;
    response.data.content.forEach(element => {
      element.isPass='';
      element.reason=''
    });
   
    projectList.value = response.data.content;


  });
}

/** 提交按钮 */
function submitForm() {
  if (!queryParams.value.checkDate) {
    proxy.$modal.msgError("缺少检查时间");
    return;
  }
  let data = {
    ...queryParams.value,
    list: projectList.value
  }
  addImprovementList(data).then(response => {
    proxy.$modal.msgSuccess("保存成功");
    // 返回上一页
    bindBack()
  });
}



const bindProjectSubmit = () => {
  projectList.value.push(deepClone(form.value))
  open.value = false;
}

const bindBack = () => {
  router.go(-1)
}


getList();
getDeptTree()




function deepClone(obj) {
  let objClone = Array.isArray(obj) ? [] : {}
  if (obj && typeof obj === 'object') {
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        // 判断ojb子元素是否为对象，如果是，递归复制
        if (obj[key] && typeof obj[key] === 'object') {
          objClone[key] = deepClone(obj[key])
        } else {
          // 如果不是，简单复制
          objClone[key] = obj[key]
        }
      }
    }
  }
  return objClone
}

</script>

<style scoped>
.header {
  padding-bottom: 20px;
}
.margin {
  margin: 80px auto;
  text-align: center;
}
</style>
